package com.geso.code_generator.utils;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public class GeneratorUtils {
	
	private static final String[] TYPE_INT_ARRAY = {"tinyint","smallint","mediumint","int","bigint"};
	private static final String[] TYPE_DOUBLE_ARRAY = {"double"};
	private static final String[] TYPE_DATE_ARRAY = {"timestamp","date","datetime","time"};
	private static final String[] TYPE_STRING_ARRAY = {"char","varchar","tinyblob","tinytext","blob","text","longtext"};
	/**
	 * 首字母大写
	 * @param word
	 * @return
	 */
	public static String upper1stLetter(String word){
        char[] cs = word.toCharArray();
        if(Character.isLetter(cs[0])){
        	cs[0]-= 32;
        }
        return String.valueOf(cs);
	}
	
	/**
	 * 表名转对象名
	 * @param tableName
	 * @return
	 */
	public static String table2ObjectName(String tableName) {
		String objectName = "";
		if(StringUtils.isNotBlank(tableName)){
			tableName = StringUtils.lowerCase(tableName);
			if(tableName.contains("_")){
				String[] strs = tableName.split("_");
				for (String string : strs) {
					objectName += upper1stLetter(string);
				}
			}else{
				objectName = upper1stLetter(tableName);
			}
		}
		return objectName;
	}
	
	/**
	 * 字段名转对象属性名
	 * @param column
	 * @return
	 */
	public static String column2Attribute(String column) {
		String attribute = "";
		if(StringUtils.isNotBlank(column)){
			column = StringUtils.lowerCase(column);
			if(column.contains("_")){
				String[] strs = column.split("_");
				String word = "";
				for (int i = 0; i < strs.length; i++) {
					word = strs[i];
					if(i != 0){
						word = upper1stLetter(word);
					}
					attribute += word; 
				}
			}else {
				attribute = column;
			}
		}
		return attribute;
	}
	
	/**
	 * 数据库类型转java基本类型
	 * @param columnType
	 * @return
	 */
	public static String columnType2AttributeType(String columnType) {
		if(ArrayUtils.contains(TYPE_INT_ARRAY, columnType)){
			return "Integer";
		}else if(ArrayUtils.contains(TYPE_DATE_ARRAY,columnType)){
			return "Date";
		}else if(ArrayUtils.contains(TYPE_STRING_ARRAY,columnType)){
			return "String";
		}else if(ArrayUtils.contains(TYPE_DOUBLE_ARRAY, columnType)){
			return "Double";
		}else{
			return columnType.toLowerCase();
		}
	}
	
	public static void main(String[] args) {
//		System.out.println(upper1stLetter("qinxiaoxiang"));
		System.out.println(column2Attribute("ACTIVITY"));
		
		System.out.println(column2Attribute("ent_id"));
	}

}
